package com.tevintao.marketingsystem.task;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;

import com.tevintao.marketingsystem.log.LogManager;
import com.tevintao.marketingsystem.services.TaskLogicService;
import com.tevintao.marketingsystem.services.ViewOperation;
import com.tevintao.marketingsystem.xposted.HookWindowManagerGlobal;
import com.tevintao.marketingsystem.xposted.WxTaskManger;

import static com.tevintao.marketingsystem.task.WxUiViewConfig.getViewByClassName;
import static com.tevintao.marketingsystem.task.WxUiViewConfig.getViewByIdAndContent;


public class SendFriendCircleTaskProcess extends SetTaskExtra {
    private static SendFriendCircleTaskProcess ourInstance = new SendFriendCircleTaskProcess();

    public static SendFriendCircleTaskProcess getInstance() {
        return ourInstance;
    }

    private SendFriendCircleTaskProcess() {
    }


    @SuppressWarnings("ResourceType")
    private void startComment(View rootView, View selfFriendView, String comment) {
        View buttonViewFaSong = rootView.findViewById(2131627671);
        if (null != buttonViewFaSong && buttonViewFaSong instanceof TextView) {
            View editViewShuRu = rootView.findViewById(2131627669);
            if (editViewShuRu instanceof EditText) {
                EditText textView = (EditText) editViewShuRu;
                if ("".equals(textView.getText().toString())) {
                    //TextView textView = (TextView) buttonViewFaSong;
                    LogManager.getInstance().logInfo("等待键盘自动输入评论内容 ");
                    ViewOperation.setText(textView, comment);
                } else {
                    TextView textViewFaSong = (TextView) buttonViewFaSong;
                    if ("发送".equals(textViewFaSong.getText().toString())) {
                        ViewOperation.clickView(buttonViewFaSong);
                        taskError(rootView.getContext(), WxTaskManger.TASK_SATE_SUCCESS, getExtra(WxUiViewConfig.KEY_FRIEND_CIRCLE_ID).toString());
                    }
                }
            }
        } else {
            View commentView = rootView.findViewById(WxUiViewConfig.CIRCLE_OF_FRIENDS_COMMENT);
            LogManager.getInstance().logInfo("commentView = " + commentView + (commentView == null ? null : commentView.getVisibility()));
            if (null != commentView && commentView.getVisibility() == View.VISIBLE) {
                TextView commentTextView = (TextView) commentView;
                String commentDes = HookWindowManagerGlobal.getViewJson(commentView).toString();
                LogManager.getInstance().logError(commentDes);
                int color = commentTextView.getTextColors().getDefaultColor();
                if (color == -1) { //刚发送的朋友圈，不能点击
                    ViewOperation.clickView(commentView);
                    LogManager.getInstance().logInfo("打开评论 ");
                } else {
                    Intent intent = new Intent(TaskLogicService.ACTION_KEY_RECEIVER);
                    Bundle bundle = new Bundle();
                    bundle.putInt(TaskLogicService.KEY_INPUT_KEY, TaskLogicService.INPUT_TYPE_BACK);
                    intent.putExtras(bundle);
                    rootView.getContext().sendBroadcast(intent);
                }

            } else {
                View menuView = selfFriendView.findViewById(WxUiViewConfig.CIRCLE_OF_FRIENDS_OPEN_MENU);
                if (null != menuView) {
                    ViewOperation.clickView(menuView);
                }
                LogManager.getInstance().logInfo("menuView=" + menuView);
            }
        }
    }

    @SuppressWarnings("ResourceType")
    @Override
    public boolean processView(String contextName, View v) {
        Object taskId = getExtra(WxUiViewConfig.KEY_FRIEND_CIRCLE_ID);
        if (null == taskId) {
            taskError(v.getContext(), WxTaskManger.TASK_SATE_EXTRA_ERROR, "没有朋友圈ID");
            return true;
        }
        if (WxUiViewConfig.MAIN_LAUNCH.equals(contextName)) {
            boolean inDiscover = WxUiViewConfig.inTab(v, WxUiViewConfig.TAB_DISCOVER);
            if (inDiscover) {
                View friendView = getViewByIdAndContent((ViewGroup) v, WxUiViewConfig.DISCOVER_LIST_ITEM_ID, "朋友圈");
                LogManager.getInstance().logInfo("friendView = " + friendView);
                if (null != friendView) {
                    ViewOperation.clickView(friendView);
                }
            }
        } else if (WxUiViewConfig.CIRCLE_OF_FRIENDS.equals(contextName)) {
//            View listView = v.findViewById(WxUiViewConfig.CIRCLE_OF_FRIENDS_LIST_VIEW);
//            LogManager.getInstance().logInfo("listView = " + listView);
//            if (null != listView && listView instanceof ListView) {
//                ListView list = (ListView) listView;
//                View selfSend = list.getChildAt(0);
//                if (null != selfSend) {
////                    WxUiViewConfig.printViewGroup((ViewGroup) selfSend);
//                    View nameView = selfSend.findViewById(2131625210);
////                    WxUiViewConfig.printViewGroup((ViewGroup) selfSend);
//                    String nickName = "";
//                    if (null != nameView && nameView instanceof TextView) {
//                        TextView nameTextView = (TextView) nameView;
//                        nickName = nameTextView.getText().toString();
//                        int textSize = (int) nameTextView.getTextSize();
//                        if (textSize == 32) {
//                            ViewOperation.moveListPosition(list, 1);
//                            return true;
//                        }
//                    }
//                    String serviceNickName = "";
//                    Object serviceNickObj = getExtra(WxUiViewConfig.KEY_NEED_CHAT_NICK_NAME);
//                    if (null != serviceNickObj) {
//                        serviceNickName = serviceNickObj.toString();
//                    }
//                    LogManager.getInstance().logInfo("nickName = " + nickName + " serviceNickName = " + serviceNickName);
//                    if (nickName.equals(serviceNickName)) {
//                        LogManager.getInstance().logInfo("朋友圈发送完毕");
//                        Object comment = getExtra(WxUiViewConfig.KEY_CHAT_CONTENT);
//                        if (null == comment || TextUtils.isEmpty(comment.toString())) {
//                            taskError(v.getContext(), WxTaskManger.TASK_SATE_SUCCESS, getExtra(WxUiViewConfig.KEY_FRIEND_CIRCLE_ID).toString());
//                        } else {
//                            startComment(v, selfSend, comment.toString());
//                        }
//                        return true;
//                    } else {
//                        ViewOperation.moveListPosition(list, 1);
//                    }
//                }
//            }
            View selectMenu = getViewByClassName((ViewGroup) v, "ImageButton");
            if (null != selectMenu) {
                ViewOperation.clickView(selectMenu);
                LogManager.getInstance().logInfo("点击了右上角的菜单按钮");
            }

        } else if ("android.view.ContextThemeWrapper".equals(contextName)) {
            View photoView = getViewByIdAndContent((ViewGroup) v, 2131624112, "照片");
            View konwView = getViewByIdAndContent((ViewGroup) v, 2131626642, "我知道了");
            if (null != photoView) {
                ViewOperation.clickView(photoView);
                LogManager.getInstance().logInfo("点击了照片按钮");
            }
            //提醒提示框   提示：你拍的照片，你的微信朋友可以看到并且评论。每个人只能看到自己朋友的评论，因此，不在你通讯录的人，不会出现在照片的评论里。
            if (null != konwView) {
                ViewOperation.clickView(konwView);
                LogManager.getInstance().logInfo("点击了我知道了   跳过提醒提示框");
            }

        } else if ("com.tencent.mm.plugin.gallery.ui.AlbumPreviewUI".equals(contextName)) {
            View gridView = v.findViewById(2131627390);
            if (null != gridView) {
                GridView gridviewView = (GridView) gridView;
                if (gridviewView.getChildCount() > 1) {
                    View childPicture = gridviewView.getChildAt(1);
                    ViewGroup childGroup = (ViewGroup) childPicture;
                    View childbox = childGroup.getChildAt(5);
                    if (null != childbox) {
                        CheckBox boxview = (CheckBox) childbox;
                        if (!boxview.isChecked()) {
                            // ViewOperation.setChecked(boxview,true);
                            ViewOperation.clickView(boxview);
                            LogManager.getInstance().logInfo("勾选了一个需要    发送朋友圈的图片");
                        }
                    }
                    //View finishView = getViewByIdAndContent((ViewGroup) v,2131624139, "完成(1\\/9)");
                    View finishView = v.findViewById(2131624139);
                    if (null != finishView) {
                        TextView finishview = (TextView) finishView;
                        if (!"完成".equals(finishview.getText().toString())) {
                            ViewOperation.clickView(finishView);
                            LogManager.getInstance().logInfo("点击了完成按钮");
                        }
                    }
                } else {
                    LogManager.getInstance().logInfo("当前页面没有照片");
                }
            }
        } else if ("com.tencent.mm.plugin.sns.ui.SnsUploadUI".equals(contextName)) {
            LogManager.getInstance().logInfo("这是    输入说说内容界面");
            //点击输入框，跳出来输入键盘，可以开始进行评论。如果检测到了发送按钮
            View buttonViewFaSong = v.findViewById(2131624139);
            if (null != buttonViewFaSong && buttonViewFaSong instanceof TextView) {
                View editViewShuRu = v.findViewById(2131627762);
                if (editViewShuRu instanceof EditText) {
                    EditText textView = (EditText) editViewShuRu;
                    if ("".equals(textView.getText().toString())) {
                        LogManager.getInstance().logInfo("等待键盘自动输入朋友圈内容 ");
                        Object idea = getExtra("contentIdea");
                        String comment;
                        if (null != idea) {
                            comment = idea.toString();
                        } else {
                            comment = "好";
                        }
                        ViewOperation.setText(textView, comment);
                    } else {
                        TextView textViewFaSong = (TextView) buttonViewFaSong;
                        if ("发送".equals(textViewFaSong.getText().toString())) {
                            ViewOperation.clickView(buttonViewFaSong);
                            LogManager.getInstance().logInfo("输入朋友圈内容之后，按下了发送按钮");
                            taskError(v.getContext(), WxTaskManger.TASK_SATE_SUCCESS, getExtra(WxUiViewConfig.KEY_FRIEND_CIRCLE_ID).toString());
                        }
                    }
                }
            }
        }
        return false;
    }
}
